package com.itx.gatewayx.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.server.ServerWebExchange;

/**
 * Web全局异常处理器
 *
 * @author itx
 */
@Slf4j
@RestControllerAdvice
public class WebExceptionHandler {

    /**
     * 处理所有未捕获的异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R<String> handleException(Exception e, ServerWebExchange exchange) {
        log.error("系统异常: {}", exchange.getRequest().getPath(), e);
        return R.fail("系统繁忙，请稍后再试");
    }

    /**
     * 处理业务异常
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public R<String> handleRuntimeException(RuntimeException e, ServerWebExchange exchange) {
        log.error("业务异常: {}", exchange.getRequest().getPath(), e);
        return R.fail(e.getMessage());
    }
} 